/**
 * 
 */
package com.mooer.app;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.mooer.MooerApplication;
import com.mooer.R;
import com.mooer.bean.User;
import com.mooer.exception.SVException;
import com.mooer.http.HttpUtils;

/**
 * 更多 ->账户设置
 * 
 * @author Minglong.Yu
 * @version 1.0
 * @created Jun 22, 2011 12:09:23 PM
 */
public class MoreAccountSetting extends Activity {
	private ImageView userPic, loginBtn, logoutBtn, registerBtn;
	private LinearLayout noLoginBar, loginBar;
	private TextView accountInfo;
	private View loginDialogLayout;
	private EditText accountInput, passwordInput;
	private CheckBox rememberCheckbox;
	private AlertDialog loginDialog;
	private boolean isFinish = false;
	private ProgressDialog progressDialog;
	private boolean remember = false;
	private SharedPreferences sp;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.more_account_setting);

		userPic = (ImageView) findViewById(R.id.user_pic);
		accountInfo = (TextView) findViewById(R.id.account_info);
		loginBtn = (ImageView) findViewById(R.id.login_btn);
		logoutBtn = (ImageView) findViewById(R.id.logout_btn);
		registerBtn = (ImageView) findViewById(R.id.register_btn);
		noLoginBar = (LinearLayout) findViewById(R.id.no_login_status_bar);
		loginBar = (LinearLayout) findViewById(R.id.login_status_bar);

		isFinish = getIntent().getBooleanExtra("isFinish", false);

		MooerApplication app = (MooerApplication) MoreAccountSetting.this
				.getApplication();
		User user = app.getUser();
		if (app.isLoggedin() && user != null && user.getId() > 0) {
			noLoginBar.setVisibility(View.GONE);
			loginBar.setVisibility(View.VISIBLE);
			userPic.setVisibility(View.VISIBLE);
			accountInfo.setText(user.getUserName());
		}

		loginBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				LayoutInflater inflater = LayoutInflater
						.from(MoreAccountSetting.this);
				loginDialogLayout = inflater.inflate(R.layout.login_dialog,
						null);
				accountInput = (EditText) loginDialogLayout
						.findViewById(R.id.account_input);
				passwordInput = (EditText) loginDialogLayout
						.findViewById(R.id.password_input);
				rememberCheckbox = (CheckBox) loginDialogLayout
						.findViewById(R.id.remember_input);

				loginDialog = new AlertDialog.Builder(MoreAccountSetting.this)
						.setTitle(
								MoreAccountSetting.this
										.getString(R.string.login))
						.setView(loginDialogLayout)
						.setPositiveButton(R.string.login,
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										String account = accountInput.getText()
												.toString();
										String password = passwordInput
												.getText().toString();
										remember = rememberCheckbox.isChecked();
										if ((account != null && !""
												.equals(account.trim()))
												&& (password != null && !""
														.equals(password))) {
											progressDialog = new ProgressDialog(
													MoreAccountSetting.this);
											progressDialog
													.setProgressStyle(ProgressDialog.STYLE_SPINNER);
											progressDialog
													.setMessage(MoreAccountSetting.this
															.getString(R.string.sending));
											progressDialog.setCancelable(false);
											progressDialog.show();
											new UserAccountTask(
													UserAccountTask.LOGIN)
													.execute(account, password);
										} else {
											Toast t = Toast
													.makeText(
															MoreAccountSetting.this,
															R.string.account_password_not_empty,
															Toast.LENGTH_LONG);
											t.show();
										}
									}
								})
						.setNegativeButton(R.string.cancel,
								new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										dialog.dismiss();
									}
								}).create();
				loginDialog.show();
				sp = MoreAccountSetting.this
						.getPreferences(Context.MODE_PRIVATE);
				accountInput.setText(sp.getString("account", null));
				passwordInput.setText(sp.getString("password", null));
				rememberCheckbox.setChecked(sp.getBoolean("remember", false));
			}
		});

		logoutBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				progressDialog = new ProgressDialog(MoreAccountSetting.this);
				progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
				progressDialog.setMessage(MoreAccountSetting.this
						.getString(R.string.sending));
				progressDialog.setCancelable(false);
				progressDialog.show();
				new UserAccountTask(UserAccountTask.LOGOUT).execute("");
			}
		});

		registerBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
			}
		});

	}

	public class UserAccountTask extends AsyncTask<String, Integer, String> {
		public static final int LOGIN = 1;
		public static final int REGISTER = 2;
		public static final int LOGOUT = 3;
		private int type;
		private boolean success = false;
		User user;

		public UserAccountTask(int type) {
			super();
			this.type = type;
		}

		@Override
		protected String doInBackground(String... params) {
			switch (type) {
			case LOGIN:
				if (params != null && params.length == 2) {
					String account = params[0];
					String password = params[1];
					try {
						user = HttpUtils.login(account, password);
						success = true;
						MooerApplication app = (MooerApplication) MoreAccountSetting.this
								.getApplication();
						app.setUser(user);
						app.setLoggedin(true);
						sp = MoreAccountSetting.this
								.getPreferences(Context.MODE_PRIVATE);
						if (remember) {
							sp.edit().putString("account", account)
									.putString("password", password)
									.putBoolean("remember", true).commit();
						} else {
							sp.edit().clear().commit();
						}
					} catch (Exception e) {
						success = false;
						e.printStackTrace();
					}
				}
				break;
			case LOGOUT:
				// HttpUtils.logout();
				MooerApplication app = (MooerApplication) MoreAccountSetting.this
						.getApplication();
				app.setUser(null);
				app.setLoggedin(false);
				success = true;
				break;
			}
			return null;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
		 */
		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			if (progressDialog != null) {
				progressDialog.cancel();
			}
			switch (type) {
			case LOGIN:
				if (success) {
					if (user.getId() > 0) {
						noLoginBar.setVisibility(View.GONE);
						loginBar.setVisibility(View.VISIBLE);
						userPic.setVisibility(View.VISIBLE);
						accountInfo.setText(user.getUserName());
						if (isFinish) {
							MoreAccountSetting.this.finish();
						}
					} else {
						Toast t = Toast.makeText(MoreAccountSetting.this,
								MoreAccountSetting.this
										.getString(R.string.login_failed),
								Toast.LENGTH_LONG);
						t.show();
					}
				}
				break;
			case LOGOUT:
				loginBar.setVisibility(View.GONE);
				noLoginBar.setVisibility(View.VISIBLE);
				userPic.setVisibility(View.GONE);
				accountInfo.setText(R.string.account_no_login);
				break;
			case REGISTER:
				break;
			}
		}

	}
}
